package lambda_expression.jdk;

import java.util.Comparator;

public class ComparatorTest {

    public static void main(String[] args) {
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return  o1 - o2;
            }
        };

        System.out.println("com.compare(11,2) = " + com.compare(2, 2));

        System.out.println("==================comparator lambda==================");
        Comparator<Integer> com2 = (first, second) -> {
            return first -second;
        };

        Comparator<Integer> com22 = Integer::compare;

        System.out.println("com2.compare(21, -1) = " + com2.compare(21, -1));

        // 如果两个字符串长度相同，就执行thenComparing这个比较器
        Comparator<String> com3 = Comparator.comparingInt(String::length)
                .thenComparing(String.CASE_INSENSITIVE_ORDER);

        String s1 = "CoikPuut";
        String s2 = "CookBuut";

        System.out.println("asciiOf(s1) = " + asciiOf('i'));  // 105
        System.out.println("asciiOf(s2) = " + asciiOf('o')); // 111
        System.out.println("com3 = " + com3.compare(s1, s2));  // -6
    }


    private static int asciiOf(char s) {
        return Integer.valueOf(s);
    }
}
